#ifndef __YUECHUAN_MACRO_H__
#define __YUECHUAN_MACRO_H__

#include <string>
#include "util.h"
#include <cassert>



/**
 * @brief likely
 * @def YUECHUAN_LIKELY(x) likely宏的封装，告诉编译器优化，条件大概率成立
 * @def YUECHUAN_UNLIKELY(x) unlikely 宏的封装，告诉编译器又换，条件大概率不成立
 */
#if defined __GUNC__ || defined __llvm__
#   define YUECHUAN_LIKELY(x)  __builtin_expect(!!(x), 1)
#   define YUECHUAN_UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#   define YUECHUAN_LIKELY(x)   (x)
#   define YUECHUAN_UNLIKELY(x)  (x)

#endif
/**
 * @brief 有关assert的宏定义
 * @details 使用自定义的backtrace来进行存储调用栈
 */
#define YUECHUAN_ASSERT(x)                                                                     \
    if (YUECHUAN_UNLIKELY(!(x)))                                                                                  \
    {                                                                                          \
        YUECHUAN_LOG_ERROR(YUECHUAN_LOG_ROOT()) << " ASSERTION :" #x                           \
                                                << "\nbacktrace: \n"                           \
                                                << yuechuan::BacktraceToString(100, 2, "   "); \
        assert(x);                                                                             \
    }
#define YUECHUAN_ASSERT2(x, w)                                                                 \
    if (YUECHUAN_UNLIKELY(!(x)))                                                                                  \
    {                                                                                          \
        YUECHUAN_LOG_ERROR(YUECHUAN_LOG_ROOT()) << " ASSERTION :" #x                           \
                                                << "\n"                                        \
                                                << #w                                          \
                                                << "\nbacktrace:\n"                            \
                                                << yuechuan::BacktraceToString(100, 2, "   "); \
        assert(x);                                                                             \
     }
#endif

